<template>
  <div class="notice-section">
    <div class="notice-header">
      <div class="notice-title">{{ title }}</div>
      <el-link v-if="showMore" type="primary" @click="viewMore">查看更多</el-link>
    </div>

    <ul class="notice-list" v-if="notices.length > 0">
      <li
        v-for="notice in notices"
        :key="notice.id"
        class="notice-item"
        @click="goToDetail(notice)"
      >
        <div v-if="notice.isNew || notice.isHot" class="notice-badge">
          {{ notice.isHot ? '热' : '新' }}
        </div>
        <div class="notice-content">{{ notice.title }}</div>
        <div class="notice-date">{{ formatDate(notice.createTime) }}</div>
      </li>
    </ul>

    <el-empty v-else description="暂无公告" :image-size="100" />
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRouter } from 'vue-router'

const props = defineProps({
  notices: {
    type: Array,
    default: () => []
  },
  title: {
    type: String,
    default: '系统公告'
  },
  showMore: {
    type: Boolean,
    default: true
  },
  limit: {
    type: Number,
    default: 5
  }
})

const router = useRouter()

// 限制显示的公告数量
const limitedNotices = computed(() => {
  return props.notices.slice(0, props.limit)
})

// 格式化日期
const formatDate = (dateString) => {
  if (!dateString) return ''
  const date = new Date(dateString)
  return date.toLocaleDateString('zh-CN')
}

// 查看公告详情
const goToDetail = (notice) => {
  router.push(`/notice/${notice.id}`)
}

// 查看更多公告
const viewMore = () => {
  router.push('/notices')
}
</script>

<style lang="scss" scoped>
.notice-section {
  background-color: white;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.notice-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 15px;

  .notice-title {
    font-weight: 600;
    font-size: 16px;
  }
}

.notice-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.notice-item {
  padding: 12px 0;
  border-bottom: 1px solid #f0f0f0;
  display: flex;
  align-items: center;
  cursor: pointer;
  transition: background-color 0.3s;

  &:last-child {
    border-bottom: none;
  }

  &:hover {
    background-color: #f9f9f9;
    margin: 0 -10px;
    padding: 12px 10px;
    border-radius: 4px;
  }

  .notice-badge {
    background-color: #f56c6c;
    color: white;
    font-size: 12px;
    padding: 2px 6px;
    border-radius: 10px;
    margin-right: 10px;
    min-width: 16px;
    text-align: center;
  }

  .notice-content {
    flex: 1;
    color: #303133;
    font-size: 14px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .notice-date {
    color: #909399;
    font-size: 13px;
    margin-left: 10px;
    white-space: nowrap;
  }
}
</style>